<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <el-form-item label="支付状态" prop="payState">
        <el-input
          v-model="queryParams.payState"
          placeholder="请输入支付状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入订单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fts:order:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fts:order:edit']"
        >修改</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fts:order:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fts:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" type="expand" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

     <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <h3>商品信息</h3>
             <div v-for="(item,index) in props.row.ftsOrderListList" :key="index" style="float:left; border: 1px solid #e3e3e3;padding: 20px;">
               <p><span>子订单号:{{ item.orderListCode }}</span></p>
               <p><b>商品名称:{{ item.goodsTitle }}</b></p>
               <div style="float: left;"><img :src="item.cover" style="width:150px;height:100px;"/></div>
               <div style="float: left;margin-left: 20px;">
                 <p><span>单价:{{ parseMoney(item.goodsPrice) }}</span></p>
                 <p><span>数量:{{ item.goodsNumber }}</span></p>
                 <p><span>规格:{{ item.goodsSpec }}</span></p>
               </div>
               <!-- <div>这里引入操作，预留</div> -->
              </div>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="总价" align="center" prop="price">
        <template slot-scope="scope">
          <span>{{ parseMoney(scope.row.price) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payState" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.pay_state" :value="scope.row.payState"/>
          </template>
        </el-table-column>
      <el-table-column label="订单状态" align="center" prop="state" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.order_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
      <el-table-column label="收货地址id" align="center" prop="shippingAddressId" />
      <el-table-column label="购买用户" align="center" prop="payUserId" >
      <template slot-scope="scope">
        <span>{{ scope.row.payUser.nickName }}</span>
      </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="logisticsOpenBox(scope.row)"
            v-hasPermi="['fts:order:edit']"
          >发货</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="refundOrderOpen(scope.row)"
            v-hasPermi="['fts:order:edit']"
          >退款处理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fts:order:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"  label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单编码" prop="orderCode" style="width: 100%;">
              <el-input v-model="form.orderCode" placeholder="请输入订单编码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="总价" prop="price">
          <el-input v-model="form.price" placeholder="请输入总价" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支付状态" prop="payState">
              <el-input v-model="form.payState" placeholder="支付状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单状态" prop="state">
              <el-input v-model="form.state" placeholder="请输入订单状态" />
            </el-form-item>
          </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
            <el-form-item label="购买用户" prop="payUserId">
                <el-input v-model="form.payUserId" placeholder="请输入购买用户" />
            </el-form-item>
        </el-col>
         <el-col :span="12">
            <el-form-item label="支付时间" prop="payTime" >
              <el-date-picker clearable
               style="width:100%;"
                v-model="form.payTime"
                type="datetime"
                value-format="yyyy-MM-dd"
                placeholder="支付时间">
              </el-date-picker>
            </el-form-item>
         </el-col>
       </el-row>
      <el-form-item label="收货地址" prop="shippingAddressId">
        <el-input v-model="form.shippingAddressId" placeholder="请输入收货地址" />
      </el-form-item>
        <el-divider content-position="center">子订单信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddFtsOrderList">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteFtsOrderList">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="ftsOrderListList" :row-class-name="rowFtsOrderListIndex" @selection-change="handleFtsOrderListSelectionChange" ref="ftsOrderList">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="订单编码" prop="orderCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderCode" placeholder="请输入订单编码" />
            </template>
          </el-table-column>
          <el-table-column label="子订单编码" prop="orderListCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderListCode" placeholder="请输入子订单编码" />
            </template>
          </el-table-column>
          <el-table-column label="商品id" prop="goodsId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsId" placeholder="请输入商品id" />
            </template>
          </el-table-column>
          <el-table-column label="商品数量" prop="goodsNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsNumber" placeholder="请输入商品数量" />
            </template>
          </el-table-column>
          <el-table-column label="商品单价" prop="goodsPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsPrice" placeholder="请输入商品单价" />
            </template>
          </el-table-column>
          <el-table-column label="商品规格" prop="goodsSpec" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsSpec" placeholder="请输入商品规格" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="发货" :visible.sync="logisticsOpen" :close-on-click-modal="false" width="800px" append-to-body>
      <el-descriptions title="订单信息">
          <el-descriptions-item label="订单号">{{form.orderCode}}</el-descriptions-item>
          <el-descriptions-item label="订单金额">{{parseMoney(form.price)}}</el-descriptions-item>
          <el-descriptions-item label="订单状态">
              <dict-tag :options="dict.type.order_state" :value="form.state"/>
          </el-descriptions-item>
          <el-descriptions-item label="收货地址">{{form.shippingAddress}}</el-descriptions-item>
      </el-descriptions>
      <hr/>
     <el-descriptions title="物流信息"></el-descriptions>
       <el-form ref="form" :model="form" :rules="rules"  label-width="80px" label-position="left">
          <el-form-item label="物流公司" prop="courierNumber" style="width: 100%;">
            <el-select v-model="form.logisticsCompany" filterable placeholder="请选择" style="width: 100%;">
               <el-option
                 v-for="item in logisticsOptions"
                 :key="item.name"
                 :label="item.name"
                 :value="item.name">
               </el-option>
             </el-select>
           </el-form-item>
             <el-form-item label="物流单号" prop="courierNumber" style="width: 100%;">
               <el-input v-model="form.courierNumber" placeholder="物流单号" />
             </el-form-item>
       </el-form>
       <div slot="footer" class="dialog-footer">
         <el-button type="primary" @click="logisticsSubmitForm">确 定</el-button>
         <el-button @click="logisticsBoxCancel">取 消</el-button>
       </div>
    </el-dialog>

    <el-dialog  title="退款处理" :visible.sync="refundOpen" :close-on-click-modal="false" width="800px" append-to-body>
        <el-descriptions title="订单信息">
            <el-descriptions-item label="订单号">{{form.orderCode}}</el-descriptions-item>
            <el-descriptions-item label="订单金额">{{parseMoney(form.price)}}</el-descriptions-item>
            <el-descriptions-item label="订单状态">
               <dict-tag :options="dict.type.order_state" :value="form.state"/>
            </el-descriptions-item>
            <el-descriptions-item label="收货地址">{{form.shippingAddress}}</el-descriptions-item>
        </el-descriptions>
        <hr/>
        <el-descriptions title="退款信息"></el-descriptions>
        <el-form ref="form" :model="form" :rules="rules"  label-width="80px" label-position="left">
           <el-form-item label="理由" prop="courierNumber" style="width: 100%;">
             <el-input  type="textarea" :rows="2" placeholder="请输入内容" v-model="form.reason">
             </el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
           <el-button type="primary" @click="refundAgree">同意退款</el-button>
           <el-button type="danger" @click="refundReject">拒绝退款</el-button>
       </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder,listLogistics,deliverGoods,refund } from "@/api/fts/order";

export default {
  name: "Order",
  dicts: ['order_state',"pay_state","refund_state"],
  data() {
    return {
      refundOpen:false,
      logisticsOptions: [],
      value: '',
      logisticsOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFtsOrderList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderList: [],
      // 子订单信息表格数据
      ftsOrderListList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        price: null,
        payState: null,
        state: null,
        shippingAddressId: null,
        payUserId: null,
        payTime: null,
        createUserId: null,
        updateUserId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    refundOrderOpen(row){
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.ftsOrderListList = response.data.ftsOrderListList;
        this.refundOpen=true;
      });
    },
    refundAgree(){
        if (this.form.id != null) {
          this.form.refundState=3;
          refund(this.form).then(res => {
            if(res.state==200){
              this.$modal.msgSuccess("退款成功");
              this.refundOpen = false;
              this.getList();
            }else{
              this.$modal.msgError(res.message);
            }
          });
        }
    },
    refundReject(){
      if (this.form.id != null) {
        this.form.refundState=4;
          refund(this.form).then(res => {
            if(res.state==200){
              this.$modal.msgSuccess("驳回成功");
              this.refundOpen = false;
              this.getList();
            }else{
              this.$modal.msgError(res.message);
            }
          });
        }
    },
    logisticsSubmitForm(){
        if(!this.form.logisticsCompany){
           this.$modal.msgError("请先选择物流公司");
           return;
        }
        if(!this.form.courierNumber){
           this.$modal.msgError("请先填写物流单号");
           return;
        }
        if (this.form.id != null) {
          deliverGoods(this.form).then(res => {
            if(res.state==200){
              this.$modal.msgSuccess("发货成功");
              this.open = false;
              this.getList();
            }else{
              this.$modal.msgError(res.message);
            }
          });
        }
    },
    logisticsOpenBox(row){
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.ftsOrderListList = response.data.ftsOrderListList;
        this.logisticsOpen=true;
      });
      this.getLogisticsList();
    },
    logisticsBoxCancel(){
      this.logisticsOpen=false;
    },
    getLogisticsList(){
      listLogistics({}).then(response => {
        this.logisticsOptions = response.rows;
        this.loading = false;
      });
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        price: 0.00,
        payState: null,
        state: null,
        shippingAddressId: null,
        payUserId: null,
        payTime: null,
        createTime: null,
        createUserId: null,
        updateTime: null,
        updateUserId: null
      };
      this.ftsOrderListList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.ftsOrderListList = response.data.ftsOrderListList;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.ftsOrderListList = this.ftsOrderListList;
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {

      });
    },
	/** 子订单信息序号 */
    rowFtsOrderListIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 子订单信息添加按钮操作 */
    handleAddFtsOrderList() {
      let obj = {};
      obj.orderCode = "";
      obj.orderListCode = "";
      obj.goodsId = "";
      obj.goodsNumber = "";
      obj.goodsPrice = "";
      obj.goodsSpec = "";
      this.ftsOrderListList.push(obj);
    },
    /** 子订单信息删除按钮操作 */
    handleDeleteFtsOrderList() {
      if (this.checkedFtsOrderList.length == 0) {
        this.$modal.msgError("请先选择要删除的子订单信息数据");
      } else {
        const ftsOrderListList = this.ftsOrderListList;
        const checkedFtsOrderList = this.checkedFtsOrderList;
        this.ftsOrderListList = ftsOrderListList.filter(function(item) {
          return checkedFtsOrderList.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFtsOrderListSelectionChange(selection) {
      this.checkedFtsOrderList = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fts/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
